home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / guigfxlib / examples / plasma / source / plasma.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  14KB  |  605 lines

  1. /*********************************************************************
  2. ----------------------------------------------------------------------
  3.  
  4.     RGBPlasma
  5.  
  6.     a test for
  7.     guigfx.library/DirectDrawTrueColor()
  8.  
  9. ----------------------------------------------------------------------
  10. *********************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <time.h>
  17.  
  18. #include <utility/tagitem.h>
  19. #include <workbench/startup.h>
  20. #include <workbench/workbench.h>
  21. #include <intuition/intuition.h>
  22. #include <guigfx/guigfx.h>
  23. #include <exec/memory.h>
  24.  
  25. #include <clib/macros.h>
  26.  
  27. #include <proto/exec.h>
  28. #include <proto/intuition.h>
  29. #include <proto/graphics.h>
  30. #include <proto/utility.h>
  31. #include <proto/guigfx.h>
  32. #include <proto/wb.h>
  33. #include <proto/dos.h>
  34. #include <proto/intuition.h>
  35. #include <proto/guigfx.h>
  36.  
  37. #include "misc.h"
  38. #include "timer.h"
  39.  
  40. /*------------------------------------------------------------------*/
  41.  
  42. long __stack = 20000;
  43. #define PROGNAME                "RGB Plasma"
  44. #define    __VERSION__                " 1.0"
  45. static char versionstring[] = "$VER: " PROGNAME __VERSION__ "";
  46. struct Library *GuiGFXBase = NULL;
  47.  
  48. /*********************************************************************
  49. ----------------------------------------------------------------------
  50.  
  51.     global
  52.  
  53. ----------------------------------------------------------------------
  54. *********************************************************************/
  55.  
  56. void CloseGlobal(void)
  57. {
  58.     CloseLibrary(GuiGFXBase);
  59.     GuiGFXBase = NULL;
  60. }
  61.  
  62. BOOL InitGlobal(void)
  63. {
  64.     GuiGFXBase = OpenLibrary("guigfx.library", GUIGFX_VERSION);
  65.  
  66.     srand(time(NULL));
  67.  
  68.     if (!GuiGFXBase)
  69.     {
  70.         GuiGFXBase = OpenLibrary("libs/guigfx.library", GUIGFX_VERSION);
  71.     }
  72.  
  73.     if (GuiGFXBase)
  74.     {
  75.         return TRUE;
  76.     }
  77.     
  78.     printf("*** could not open guigfx.library v%d.\n", GUIGFX_VERSION);
  79.  
  80.     CloseGlobal();
  81. }
  82.  
  83.  
  84. /*********************************************************************
  85. ----------------------------------------------------------------------
  86.  
  87.     void UpdateWindowParameters(mvwindow)
  88.  
  89.     get current window parameters
  90.  
  91. ----------------------------------------------------------------------
  92. *********************************************************************/
  93.  
  94. void UpdateWindowParameters(struct mvwindow *win)
  95. {
  96.     win->winleft = win->window->LeftEdge;
  97.     win->wintop = win->window->TopEdge;
  98.     win->winwidth = win->window->Width;
  99.     win->winheight = win->window->Height;
  100.     win->innerleft = win->window->BorderLeft;
  101.     win->innertop = win->window->BorderTop;
  102.     win->innerwidth = win->winwidth - win->innerleft - win->window->BorderRight;
  103.     win->innerheight = win->winheight - win->innertop - win->window->BorderBottom;
  104. }
  105.  
  106.  
  107. /*********************************************************************
  108. ----------------------------------------------------------------------
  109.  
  110.     DeleteMVWindow (mvwindow)
  111.  
  112. ----------------------------------------------------------------------
  113. *********************************************************************/
  114.  
  115. void DeleteMVWindow (struct mvwindow *win)
  116. {
  117.     if (win)
  118.     {
  119.         if (win->window)
  120.         {
  121.             Forbid();
  122.  
  123.             if (win->appwindow && win->appmsgport)
  124.             {
  125.                 struct AppMessage *appmsg;
  126.             
  127.                 while (appmsg = (struct AppMessage *) GetMsg(win->appmsgport))
  128.                 {
  129.                     ReplyMsg((struct Message *) appmsg);
  130.                 }
  131.             
  132.                 RemoveAppWindow(win->appwindow);
  133.             }
  134.     
  135.             DeleteMsgPort(win->appmsgport);
  136.  
  137.             Permit();
  138.  
  139.             CloseWindow(win->window);
  140.         }
  141.         
  142.         free(win);
  143.     }
  144. }
  145.  
  146.  
  147.  
  148. /*********************************************************************
  149. ----------------------------------------------------------------------
  150.  
  151.     mvwindow = CreateMVWindow (screen)
  152.  
  153. ----------------------------------------------------------------------
  154. *********************************************************************/
  155.  
  156. #define inserttag(x,t,d) {(x)->ti_Tag=(t);((x)++)->ti_Data=(ULONG)(d);}
  157.  
  158. struct mvwindow *CreateMVWindow (struct Screen *scr)
  159. {
  160.     struct mvwindow *win;
  161.     
  162.     if (win = malloc(sizeof(struct mvwindow)))
  163.     {
  164.         BOOL success = FALSE;
  165.         struct TagItem *taglist;
  166.  
  167.         memset(win, 0, sizeof(struct mvwindow));
  168.  
  169.         win->screen = scr;
  170.  
  171.         if(win->appmsgport = CreateMsgPort())
  172.         {
  173.             win->appSignal = 1L << win->appmsgport->mp_SigBit;
  174.  
  175.             if(taglist = AllocateTagItems(20))
  176.             {
  177.                 UWORD visibleWidth, visibleHeight, visibleMidX, visibleMidY;
  178.                 UWORD visibleLeft, visibleTop;
  179.                 WORD winwidth, winheight, wintop, winleft;
  180.                 struct TagItem *tp = taglist;
  181.                 ULONG modeID;
  182.         
  183.                 visibleWidth = scr->Width;
  184.                 visibleHeight = scr->Height;
  185.         
  186.                 if ((modeID = GetVPModeID(&scr->ViewPort)) != INVALID_ID)
  187.                 {
  188.                     DisplayInfoHandle dih;
  189.             
  190.                     if(dih = FindDisplayInfo(modeID))
  191.                     {
  192.                         struct DimensionInfo di;    
  193.             
  194.                         if(GetDisplayInfoData(dih, (UBYTE*) &di, sizeof(di), DTAG_DIMS, modeID))
  195.                         {
  196.                             visibleWidth = di.TxtOScan.MaxX - di.TxtOScan.MinX;
  197.                             visibleHeight = di.TxtOScan.MaxY - di.TxtOScan.MinY;
  198.                         }
  199.                     }
  200.                 }
  201.         
  202.                 visibleLeft = -scr->ViewPort.DxOffset;
  203.                 visibleTop = -scr->ViewPort.DyOffset;
  204.  
  205.                 visibleMidX = visibleWidth/2 - scr->ViewPort.DxOffset;
  206.                 visibleMidY = visibleHeight/2 - scr->ViewPort.DyOffset;
  207.         
  208.                 winwidth = visibleWidth / 2;
  209.                 winheight = visibleHeight / 2;
  210.                 inserttag(tp, WA_Width, winwidth);
  211.                 inserttag(tp, WA_Height, winheight);
  212.         
  213.                 winleft = visibleMidX - winwidth/2;
  214.                 wintop = visibleMidY - winheight/2;
  215.                 inserttag(tp, WA_Left, winleft);
  216.                 inserttag(tp, WA_Top, wintop);
  217.     
  218.                 win->otherwinpos[0] = visibleLeft;
  219.                 win->otherwinpos[1] = visibleTop;
  220.                 win->otherwinpos[2] = visibleWidth;
  221.                 win->otherwinpos[3] = visibleHeight;
  222.  
  223.                 inserttag(tp, WA_Zoom, &win->otherwinpos);
  224.  
  225.                 inserttag(tp, WA_PubScreen, scr);
  226.         
  227.                 inserttag(tp, WA_Title, DEFAULT_WINTITLE);
  228.         
  229.                 inserttag(tp, WA_NewLookMenus, TRUE);
  230.         
  231.         
  232.                 inserttag(tp, WA_Flags, 
  233.                             WFLG_SIZEBBOTTOM | WFLG_DRAGBAR | 
  234.                             WFLG_SIZEGADGET | WFLG_DEPTHGADGET | WFLG_ACTIVATE |
  235.                             WFLG_CLOSEGADGET | WFLG_SIMPLE_REFRESH);
  236.  
  237.                 inserttag(tp, WA_IDCMP, 
  238.                             IDCMP_CLOSEWINDOW | IDCMP_MENUPICK | 
  239.                             IDCMP_NEWSIZE | IDCMP_ACTIVEWINDOW |
  240.                             IDCMP_VANILLAKEY | IDCMP_RAWKEY | IDCMP_MOUSEBUTTONS);
  241.             
  242.                 inserttag(tp, WA_MinWidth, DEFAULT_MINWIDTH);
  243.                 inserttag(tp, WA_MinHeight, DEFAULT_MINHEIGHT);
  244.                 inserttag(tp, WA_MaxWidth, DEFAULT_MAXWIDTH);
  245.                  inserttag(tp, WA_MaxHeight, DEFAULT_MAXHEIGHT);
  246.  
  247.                 inserttag(tp, TAG_DONE, 0);
  248.         
  249.                 if(win->window = OpenWindowTagList(NULL, taglist))
  250.                 {
  251.                     win->idcmpSignal = 1L << win->window->UserPort->mp_SigBit;
  252.                     UpdateWindowParameters(win);
  253.  
  254.                     win->appwindow = AddAppWindow(0, 0, win->window, win->appmsgport, NULL);
  255.  
  256.                     success = TRUE;
  257.                 }
  258.         
  259.                 FreeTagItems(taglist);
  260.             }
  261.         }
  262.         
  263.         if (!success)
  264.         {
  265.             DeleteMVWindow(win);
  266.             win = NULL;
  267.         }
  268.     }
  269.  
  270.     return win;
  271. }
  272.  
  273. #undef inserttag
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. #define    BUFWIDTHB    7
  282. #define BUFWIDTH    (1 << BUFWIDTHB)
  283. #define BUFHEIGHT    96
  284.  
  285. ULONG __far scrbuf[2][BUFWIDTH*BUFHEIGHT];
  286.  
  287. double sd1, sd2, sd3, sd4, sd5, sd6;
  288. double dd1, dd2, dd3, dd4, dd5, dd6;
  289. int sa1, sa2, sa3;
  290.  
  291. #define    TABLEN 1024
  292. #define    TABLEN2    333
  293.  
  294. int presinus1[TABLEN+BUFWIDTH];
  295. int presinus2[TABLEN+BUFWIDTH];
  296. int presinus3[TABLEN+BUFWIDTH];
  297.  
  298. void initeffekt (void)
  299. {
  300.     int i;
  301.     double s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
  302.     double s7 = 0, s8 = 0, s9 = 0, s10 = 0, s11 = 0, s12 = 0;
  303.     double ds1, ds2, ds3, ds4, ds5, ds6;
  304.     double ds7, ds8, ds9, ds10, ds11, ds12;
  305.     double pi2 = 6.283056;
  306.  
  307.     sa1 = rand() % TABLEN;
  308.     sa2 = rand() % TABLEN;
  309.     sa3 = rand() % TABLEN;
  310.  
  311.     ds1 = pi2 * (3 + (rand() % 6)) / TABLEN;
  312.     ds2 = pi2 * (3 + (rand() % 6)) / TABLEN;
  313.     ds3 = pi2 * (3 + (rand() % 6)) / TABLEN;
  314.     ds4 = pi2 * (3 + (rand() % 6)) / TABLEN;
  315.     ds5 = pi2 * (3 + (rand() % 6)) / TABLEN;
  316.     ds6 = pi2 * (3 + (rand() % 6)) / TABLEN;
  317.     ds7 = pi2 * (3 + (rand() % 6)) / TABLEN;
  318.     ds8 = pi2 * (3 + (rand() % 6)) / TABLEN;
  319.     ds9 = pi2 * (3 + (rand() % 6)) / TABLEN;
  320.     ds10 = pi2 * (3 + (rand() % 6)) / TABLEN;
  321.     ds11 = pi2 * (3 + (rand() % 6)) / TABLEN;
  322.     ds12 = pi2 * (3 + (rand() % 6)) / TABLEN;
  323.     
  324.     for (i = 0; i < TABLEN + BUFWIDTH; ++i)
  325.     {
  326.         presinus1[i] = ((sin(s1) + cos(s2) + sin(s3) + sin(s4)) * 32) + 128;
  327.         presinus2[i] = ((sin(s5) + cos(s6) + sin(s7) + cos(s8)) * 32) + 128;
  328.         presinus3[i] = ((sin(s9) + cos(s10) + cos(s11) + cos(s12)) * 32) + 128;
  329.         s1 += ds1;
  330.         s2 += ds2;
  331.         s3 += ds3;
  332.         s4 += ds4;
  333.         s5 += ds5;
  334.         s6 += ds6;
  335.         s7 += ds7;
  336.         s8 += ds8;
  337.         s9 += ds9;
  338.         s10 += ds10;
  339.         s11 += ds11;
  340.         s12 += ds12;
  341.     }
  342.  
  343.     sd1 = drand48() * 0.1;
  344.     sd2 = drand48() * 0.1;
  345.     sd3 = drand48() * 0.1;
  346.     sd4 = drand48() * 0.1;
  347.     sd5 = drand48() * 0.1;
  348.     sd6 = drand48() * 0.1;
  349.  
  350.     dd1 = drand48() * 0.06 - 0.03;
  351.     dd2 = drand48() * 0.06 - 0.03;
  352.     dd3 = drand48() * 0.06 - 0.03;
  353.     dd4 = drand48() * 0.06 - 0.03;
  354.     dd5 = drand48() * 0.06 - 0.03;
  355.     dd6 = drand48() * 0.06 - 0.03;
  356. }
  357.  
  358.  
  359. ULONG *effekt (void)
  360. {
  361.     static int doub = 0;
  362.     ULONG *buffer;
  363.  
  364.     double pi2 = 6.283056;
  365.     UBYTE *bp;
  366.     double d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0;
  367.     static double sin1 = 0, sin2 = 0, sin3 = 0, sin4 = 0, sin5 = 0, sin6 = 0;
  368.  
  369.     int s1, s2, s3;
  370.     int *pp1, *pp2, *pp3;
  371.     int xx, yy;
  372.  
  373.  
  374.     buffer = &scrbuf[doub][0];
  375.     doub ^= 1;
  376.  
  377.  
  378.     sin1 += sd1; if (sin1 > pi2) sin1 -= pi2;
  379.     sin2 += sd2; if (sin2 > pi2) sin2 -= pi2;
  380.     sin3 += sd3; if (sin3 > pi2) sin3 -= pi2;
  381.     sin4 += sd4; if (sin4 > pi2) sin4 -= pi2;
  382.     sin5 += sd5; if (sin5 > pi2) sin5 -= pi2;
  383.     sin6 += sd6; if (sin6 > pi2) sin6 -= pi2;
  384.  
  385.     for (yy = 0; yy < BUFHEIGHT; ++yy)
  386.     {
  387.         bp = (UBYTE *) (buffer + (yy << BUFWIDTHB));
  388.  
  389.         s1 = (int)(sa1 + (sin(sin1 + d1) + sin(sin2 + d2)) * TABLEN2/4 + TABLEN2/2) & (TABLEN - 1);
  390.         s2 = (int)(sa2 + (sin(sin3 + d3) + sin(sin4 + d4)) * TABLEN2/4 + TABLEN2/2) & (TABLEN - 1);
  391.         s3 = (int)(sa3 + (sin(sin5 + d5) + sin(sin6 + d6)) * TABLEN2/4 + TABLEN2/2) & (TABLEN - 1);
  392.  
  393.         d1 += sin(dd1) * .15;
  394.         d2 += dd2;
  395.         d3 += sin(dd3) * .15;
  396.         d4 += dd4;
  397.         d5 += sin(dd5) * .15;
  398.         d6 += dd5;
  399.         
  400.         pp1 = presinus1 + s1;
  401.         pp2 = presinus2 + s2;
  402.         pp3 = presinus3 + s3;
  403.     
  404.         for (xx = 0; xx < BUFWIDTH; ++xx)
  405.         {
  406.             bp++;
  407.             *bp++ = *pp1++;
  408.             *bp++ = *pp2++;
  409.             *bp++ = *pp3++;
  410.         }
  411.     }
  412.     
  413.     sa1 += 2;
  414.     sa2 += 2;
  415.     sa3 += 2;
  416.  
  417.     return buffer;
  418. }
  419.  
  420.  
  421. /*********************************************************************
  422. ----------------------------------------------------------------------
  423.  
  424.     mysticube
  425.     mainloop
  426.  
  427. ----------------------------------------------------------------------
  428. *********************************************************************/
  429.  
  430. void mainloop(struct Screen *scr, struct mvwindow *win)
  431. {
  432.     APTR timehandle = NULL;
  433.     int time;
  434.     char text[30];
  435.     BOOL timer = FALSE;
  436.     APTR ddh;
  437.     
  438.     ULONG *buffer;
  439.     APTR drawhandle;
  440.     BOOL finish = FALSE;
  441.     ULONG signals;
  442.     struct IntuiMessage *imsg;
  443.  
  444.     SetABPenDrMd(win->window->RPort, 1,2, JAM2);
  445.  
  446.     drawhandle = ObtainDrawHandle(NULL, win->window->RPort,
  447.             scr->ViewPort.ColorMap, OBP_Precision, PRECISION_EXACT, TAG_DONE);
  448.  
  449.     ddh = CreateDirectDrawHandle(drawhandle, BUFWIDTH, BUFHEIGHT, win->innerwidth, win->innerheight, NULL);
  450.  
  451.     initeffekt();
  452.  
  453.     if (ddh && drawhandle)
  454.     {
  455.         do
  456.         {
  457.     
  458.             signals = SetSignal(0, win->idcmpSignal | win->appSignal);
  459.     
  460.     
  461.             //    IDCMP message loop
  462.     
  463.             if (signals & win->idcmpSignal)
  464.             {
  465.                 while (imsg = (struct IntuiMessage *) GetMsg(win->window->UserPort))
  466.                 {
  467.                     ULONG iclass = imsg->Class;
  468.                     ULONG icode = imsg->Code;
  469.             
  470.                     ReplyMsg((struct Message *) imsg);
  471.             
  472.                     switch (iclass)
  473.                     {
  474.                         case CLOSEWINDOW:
  475.                             finish = TRUE;
  476.                             break;
  477.             
  478.                         case NEWSIZE:
  479.                             if ((win->window->Width != win->winwidth) ||
  480.                                 (win->window->Height != win->winheight))
  481.                             {
  482.                                 UpdateWindowParameters(win);
  483.                                 DeleteDirectDrawHandle(ddh);
  484.                                 ddh = CreateDirectDrawHandle(drawhandle, BUFWIDTH,BUFHEIGHT, win->innerwidth, win->innerheight, NULL);
  485.                             }
  486.                             break;
  487.     
  488.                         case VANILLAKEY:
  489.                             switch (icode)
  490.                             {
  491.                                 case 27:
  492.                                     finish = TRUE;
  493.                                     break;
  494.     
  495.                                 case 32:
  496.                                     initeffekt();
  497.                                     break;
  498.     
  499.                                 case 't':
  500.                                     timer = timer ? FALSE : TRUE;
  501.                                     break;
  502.                             }
  503.                     }
  504.             
  505.                 }
  506.             }
  507.     
  508.     
  509.             //    get 1/1000 seconds since last frame
  510.     
  511.             if (timehandle)
  512.             {
  513.                 time = timerstop(timehandle);
  514.             }
  515.             else
  516.             {
  517.                 time = 0;
  518.             }
  519.     
  520.     
  521.             //    start timer
  522.     
  523.             timehandle = timerstart();
  524.     
  525.     
  526.             //    draw
  527.  
  528.             buffer = effekt();
  529.     
  530.             if (buffer)
  531.             {
  532.                 DirectDrawTrueColor(ddh, buffer, win->innerleft, win->innertop, NULL);
  533.     
  534.                 if (timer)
  535.                 {
  536.                     //    display frames per second
  537.         
  538.                     Move(win->window->RPort, win->innerleft, 
  539.                         win->innertop + win->innerheight - win->window->RPort->TxHeight + win->window->RPort->TxBaseline);
  540.                     sprintf(text, "fps: %ld", (ULONG) ((double) 1000 / (double) time));
  541.                     Text(win->window->RPort, text, strlen(text));
  542.                 }
  543.             }
  544.     
  545.     
  546.         } while (finish == FALSE);
  547.     }
  548.  
  549.     if (ddh) DeleteDirectDrawHandle(ddh);
  550.     if (drawhandle) ReleaseDrawHandle(drawhandle);
  551. }
  552.  
  553.  
  554.  
  555. /*********************************************************************
  556. ----------------------------------------------------------------------
  557.  
  558.     main
  559.  
  560. ----------------------------------------------------------------------
  561. *********************************************************************/
  562.  
  563. ULONG main (int argc, char **argv)
  564. {
  565.     ULONG result;
  566.  
  567.     if(InitGlobal())
  568.     {
  569.         struct mvwindow *window;
  570.         struct Screen *defscreen;
  571.  
  572.         if (defscreen = LockPubScreen(NULL))
  573.         {
  574.             if (window = CreateMVWindow(defscreen))
  575.             {
  576.                 SetTaskPri(FindTask(NULL), TASKPRIO);
  577.                 mainloop(defscreen, window);
  578.                 DeleteMVWindow(window);
  579.                 result = 0;
  580.             }
  581.             else
  582.             {
  583.                 printf("*** window could not be opened\n");
  584.                 result = 20;
  585.             }
  586.  
  587.             UnlockPubScreen(NULL, defscreen);
  588.         }
  589.         else
  590.         {
  591.             printf("*** pubscreen could not be locked\n");
  592.             result = 20;
  593.         }
  594.         
  595.         CloseGlobal();
  596.     }
  597.     else
  598.     {
  599.         printf("*** global initialization failed\n");
  600.         result = 20;
  601.     }
  602.     
  603.     return result;
  604. }
  605.